package com.kindness.frate.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.kindness.frate.animate.AnimateUtils;

/**
 * Created by Administrator on 2017/7/26 0026.
 */
public class PullZoomScrollView extends ScrollView {

    private boolean isLoaded = false;
    private int mScreenHeight;//整个手机屏幕的高度，这是为了初始化该View时设置mTopView用的
    private int mTopViewHeight;//这个就是mTopView的高度
    private boolean isTouchOne = false;
    private float startY = 0;//点击的第一个点的Y坐标
    private float distance = 0;//向下滑动到释放的高度差
    private ValueAnimator animator; // 当抬起手指后还原原来的缩放位置
    private static final int RATIO = 3; // 下拉比例

    private LinearLayout mParentView;//布局的父布局，ScrollView内部只能有一个根ViewGroup，就是此View
    private ViewGroup mTopView;//这个是带背景的上半部分的View，下半部分的View用不到的

    public PullZoomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        this.setOverScrollMode(View.OVER_SCROLL_NEVER);
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);
        mScreenHeight = metrics.heightPixels;
        mTopViewHeight = mScreenHeight / 2 - (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, getContext().getResources().getDisplayMetrics());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (!isLoaded) {
            mParentView = (LinearLayout) getChildAt(0);
            if (mParentView.getChildCount() > 0) {
                mTopView = (ViewGroup) mParentView.getChildAt(0);
                mTopView.getLayoutParams().height = mTopViewHeight;
            }
            isLoaded = true;
        }
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if(getScrollY() >= 0){
            int y = t / RATIO;
            y = y < 0 ? 0 : y;
            mTopView.setTranslationY(y);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_MOVE:{
                if(getScrollY() == 0){
                    if(!isTouchOne){
                        startY = ev.getY();
                        isTouchOne = true;
                    }
                }
                distance = ev.getY() - startY;
                if(getScrollY() == 0) {
                    scaleImage((int) distance / RATIO);
                    if(distance <= 0){
                        break;
                    }
                    return true;
                }
            }
            break;
            case MotionEvent.ACTION_UP:{
                if(getScrollY() == 0 && distance > 0){
                    reset();
                }
                isTouchOne = false;
            }
            break;
        }
        return super.onTouchEvent(ev);
    }

    public void scaleImage(int size){
        int height = mTopViewHeight + size;
        height = height < mTopViewHeight ? mTopViewHeight : height;
        mTopView.getLayoutParams().height = height;
        mTopView.requestLayout();
    }

    /**
     * 主要用于释放手指后的回弹效果
     */
    private void reset() {
        if(animator != null && animator.isRunning()){
            animator.cancel();
        }
        animator = new AnimateUtils().valueAnimateChange((int) distance / RATIO, 0, 150, animation -> {
            float value = (float) animation.getAnimatedValue();
            scaleImage((int) value);
        });
    }

}
